/*
 * @Description: 
 * @Author: paddies
 * @Date: 2024-11-06 11:30:46
 * @LastEditors: paddies
 * @LastEditTime: 2025-01-19 02:02:57
 * @FilePath: /luntai-supplier-app/Users/paddies/Desktop/code/A-team/luntai/shared_components/lib/luntai-components/build-shop-order-card/buildShopOrderCard.dart
 */
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:shared_components/build-common/buildCommon.dart';
import 'package:shared_components/build-image/buildImage.dart';
import 'package:shared_components/build-line/buildLine.dart';

Color getStatusColor(String code) {
  if (code == "DFH") {
    return Colors.red;
  }
  return Color(0xff666666);
}

getStateName(String code, dynamic item) {
  var map = {
    "DFK": "待付款",
    "DFH": "待发货",
    "DSH": "待收货",
    "YWC": "已完成",
    "YSH": "售后中"
  };
  if (item["refundDone"].toString() == "1" && code == "YSH") {
    return "已售后";
  }

  return showText(map[code]);
}

String calQuantity(dynamic item) {
  var list = (item["orderItems"] ?? []) as List;
  var count = 0;
  for (var i = 0; i < list.length; i++) {
    var quantity = int.parse(list[i]["quantity"].toString());
    count += quantity;
  }
  return count.toString();
}

Widget buildItem(dynamic fItem, dynamic item) {
  return Container(
    width: double.infinity,
    height: 120,
    padding: EdgeInsets.only(left: 10, right: 10),
    // color: Colors.blue,
    child: Row(
      children: [
        Visibility(
          visible: true,
          // visible: item["orderType"].toString() == "SHOP",
          child: Container(
              width: 110,
              height: double.infinity,
              alignment: Alignment.center,
              child: SizedBox(
                width: 110,
                height: 110,
                child: buildImage(
                    type: "ClipRect",
                    radius: 6,
                    url: item["skuPicUrl"].toString(),
                    fit: BoxFit.fill),
              )),
        ),
        Expanded(
            flex: 1,
            child: Container(
              width: double.infinity,
              height: double.infinity,
              child: Padding(
                padding:
                    EdgeInsets.only(left: 10, top: 10, bottom: 10, right: 10),
                // padding: const EdgeInsets.symmetric(horizontal: 10),
                child: Column(
                  children: [
                    Expanded(
                        flex: 1,
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: Align(
                            alignment: Alignment.centerLeft,
                            child: Text(
                              showText(item["supplierName"]),
                              textAlign: TextAlign.center,
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: const TextStyle(
                                  fontSize: 15,
                                  fontWeight: FontWeight.bold,
                                  color: Color(0xff1D1D1F)),
                            ),
                          ),
                        )),
                    Expanded(
                        flex: 1,
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: Align(
                            alignment: Alignment.centerLeft,
                            child: Text(
                              showText(item["productName"]),
                              textAlign: TextAlign.center,
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: const TextStyle(
                                  fontSize: 14,
                                  fontWeight: FontWeight.w500,
                                  color: Color(0xff1D1D1F)),
                            ),
                          ),
                        )),
                    Expanded(
                        flex: 1,
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: Align(
                            alignment: Alignment.centerLeft,
                            child: Text(
                              showText(item["productName2"]),
                              textAlign: TextAlign.center,
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: const TextStyle(
                                  fontSize: 14,
                                  fontWeight: FontWeight.w500,
                                  color: Color(0xff9c9c9c)),
                            ),
                          ),
                        )),
                    Expanded(
                      flex: 1,
                      child: Container(
                        width: double.infinity,
                        height: double.infinity,
                        padding: const EdgeInsets.only(right: 0),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            // const Text(
                            //   "实付款 ",
                            //   style: TextStyle(
                            //       color: Color(0xff1D1D1F),
                            //       fontSize: 12,
                            //       fontWeight: FontWeight.bold),
                            // ),
                            // const Padding(
                            //   padding: EdgeInsets.only(top: 2),
                            //   child: Text(
                            //     "¥",
                            //     style: TextStyle(
                            //       fontSize: 18,
                            //       color: Color(0xffF03C36),
                            //     ),
                            //   ),
                            // ),

                            Text(
                              "x${item["quantity"]}  ",
                              style: const TextStyle(
                                  color: Color(0xff999999), fontSize: 15),
                            ),

                            Text(
                              "¥" + item["actualPrice"].toString(),
                              style: const TextStyle(
                                  fontWeight: FontWeight.bold,
                                  // color: Color(0xffF03C36),
                                  fontSize: 18),
                            )
                          ],
                        ),
                      ),
                    )
                  ],
                ),
              ),
            )),
        Visibility(
            visible: item["canRefund"] == true,
            child: Container(
              width: 90,
              height: double.infinity,
              // color: Colors.red,
              alignment: Alignment.center,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  GestureDetector(
                      onTap: () async {
                        // controller.openAfterSales(fItem, item);
                      },
                      child: Container(
                        width: 70,
                        height: 35,
                        margin: const EdgeInsets.only(left: 8, right: 8),
                        decoration: const BoxDecoration(
                          //边框
                          color: Color(0xffdf8e46),
                          borderRadius:
                              BorderRadius.all(Radius.circular(10.0)), //圆角
                        ),
                        alignment: Alignment.center,
                        child: const Text(
                          "申请售后",
                          style: TextStyle(color: Colors.white, fontSize: 13),
                        ),
                      ))
                ],
              ),
            ))
      ],
    ),
  );
}

List getOrderItems(dynamic item) {
  List list = [];
  if (item["orderItems"] != null) {
    list = item["orderItems"] ?? [];
  }
  print("商品====");
  print(item);
  return list;
}

buildShopOrderCard(
    {dynamic item,
    Function? onCardClick,
    List<Widget> buttonWidgets = const []}) {
  return GestureDetector(
      onTap: () {
        if (onCardClick != null) {
          onCardClick();
        }
        // controller.toOrderDetailPage(
        //     orderNo: item['orderNo'].toString(),
        //     refundOrderNo: item['refundOrderNo'].toString(),
        //     stateName: getStateName(code, item).toString());
      },
      child: Container(
        margin: const EdgeInsets.only(top: 10, bottom: 5),
        decoration: const BoxDecoration(
          color: Colors.white, //圆角
        ),
        child: Column(
          children: [
            Container(
              width: double.infinity,
              // height: 180,
              child: Column(
                children: [
                  // 订单和状态
                  Container(
                    width: double.infinity,
                    height: 50,
                    padding: const EdgeInsets.only(left: 15, right: 15),
                    // color: Colors.blue,
                    child: Row(
                      children: [
                        Expanded(
                            child: Container(
                          width: double.infinity,
                          height: double.infinity,
                          // color: Colors.teal,
                          alignment: Alignment.centerLeft,

                          child: Text(
                            item["orderNo"].toString(),
                            overflow: TextOverflow.ellipsis,
                            maxLines: 1,
                            style: TextStyle(
                                color: Color(0xff7E7D83), fontSize: 14),
                          ),
                        )),
                        Container(
                          width: 100,
                          height: double.infinity,
                          // color: Colors.green,
                          alignment: Alignment.centerRight,
                          child: Text(
                            getStateName(item["state"].toString(), item),
                            // "待付款",
                            style: TextStyle(
                                fontSize: 13,
                                color: getStatusColor(item["state"].toString()),
                                fontWeight: FontWeight.bold),
                          ),
                        )
                      ],
                    ),
                  ),
                  buildLine(),
                  ...getOrderItems(item).map((itemDetail) {
                    return buildItem(item, itemDetail);
                  }),
                  buildLine()
                ],
              ),
            ),
            const SizedBox(
              height: 10,
            ),
            Container(
                width: double.infinity,
                height: 50,
                padding: EdgeInsets.only(left: 10, right: 10),
                child: Row(
                  children: [
                    // Column(
                    //   mainAxisAlignment: MainAxisAlignment.center,
                    //   crossAxisAlignment: CrossAxisAlignment.start,
                    //   children: [
                    Text(
                      "共 ${calQuantity(item)} 件商品",
                      // "共 " + item["quantity"].toString() + " 件商品",
                      style: const TextStyle(
                          fontWeight: FontWeight.w500,
                          fontSize: 14,
                          color: Color(0xff666666)),
                    ),
                    SizedBox(
                      width: 15,
                    ),
                    // Text(
                    //   "合计",
                    //   style: const TextStyle(
                    //       fontWeight: FontWeight.w500,
                    //       fontSize: 14,
                    //       color: Color(0xff666666)),
                    // ),
                    // SizedBox(
                    //   width: 5,
                    // ),
                    // Text(
                    //   "¥${showText(item["actualPrice"])}",
                    //   style: const TextStyle(
                    //       fontWeight: FontWeight.bold,
                    //       fontSize: 16,
                    //       color: Color(0xffFF3333)),
                    // ),
                    Expanded(
                        child: Container(
                      width: double.infinity,
                      height: double.infinity,
                      // color: Colors.red,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [...buttonWidgets],
                      ),
                    ))
                  ],
                )

                // Stack(
                //   children: [
                //     Padding(
                //         padding: EdgeInsets.only(left: 10),
                //         child: Align(
                //           alignment: Alignment.centerLeft,
                //           child: Column(
                //             mainAxisAlignment: MainAxisAlignment.center,
                //             crossAxisAlignment: CrossAxisAlignment.start,
                //             children: [
                //               Text(
                //                 "共 ${calQuantity(item)} 件商品",
                //                 // "共 " + item["quantity"].toString() + " 件商品",
                //                 style: const TextStyle(
                //                     fontWeight: FontWeight.w500,
                //                     fontSize: 13,
                //                     color: Color(0xffFF3333)),
                //               ),
                //               Text(
                //                 "合计: ¥${showText(item["actualPrice"])}",
                //                 style: const TextStyle(
                //                     fontWeight: FontWeight.w500,
                //                     fontSize: 13,
                //                     color: Color(0xffFF3333)),
                //               )
                //             ],
                //           ),
                //         )),
                //     Row(
                //       mainAxisAlignment: MainAxisAlignment.end,
                //       crossAxisAlignment: CrossAxisAlignment.center,
                //       children: [
                //         //待发货/已完成
                //         Visibility(
                //             visible:
                //                 item["state"] == "DFH" || item["state"] == "YWC",
                //             child: GestureDetector(
                //                 onTap: () {
                //                   // controller.toOrderDetailPage(
                //                   //     orderNo: item['orderNo'].toString(),
                //                   //     stateName:
                //                   //         getStateName(code, item).toString());
                //                 },
                //                 child: Container(
                //                   width: 70,
                //                   height: 35,
                //                   margin:
                //                       const EdgeInsets.only(bottom: 10, right: 8),
                //                   decoration: const BoxDecoration(
                //                     //边框
                //                     color: Color(0xfff3f3f3),
                //                     borderRadius: BorderRadius.all(
                //                         Radius.circular(10.0)), //圆角
                //                   ),
                //                   alignment: Alignment.center,
                //                   child: const Text(
                //                     "订单详情",
                //                     style: TextStyle(
                //                         color: Color(0xff333333), fontSize: 13),
                //                   ),
                //                 ))),
                //         // 待付款
                //         Visibility(
                //             visible: item["state"] == "DFK",
                //             child: GestureDetector(
                //                 onTap: () {
                //                   // controller.openPay(
                //                   //     orderNo: item["orderNo"].toString(),
                //                   //     actualPrice: item["actualPrice"].toString());
                //                 },
                //                 child: Container(
                //                   width: 70,
                //                   height: 35,
                //                   margin:
                //                       const EdgeInsets.only(bottom: 10, right: 8),
                //                   decoration: const BoxDecoration(
                //                     //边框
                //                     color: Color(0xff3B4BF4),
                //                     borderRadius: BorderRadius.all(
                //                         Radius.circular(10.0)), //圆角
                //                   ),
                //                   alignment: Alignment.center,
                //                   child: const Text(
                //                     "去付款",
                //                     style: TextStyle(
                //                         color: Colors.white, fontSize: 13),
                //                   ),
                //                 ))),

                //         // 待收货
                //         Visibility(
                //             visible: item["state"] == "DSH",
                //             child: GestureDetector(
                //                 onTap: () {
                //                   // Get.toNamed("/webview", arguments: {
                //                   //   "title": "查看物流",
                //                   //   "url":
                //                   //       "https://m.kuaidi100.com/app/query/?coname=dbh&nu=${item["deliverySn"]}&com=${item["deliveryCorpSn"]}"
                //                   // });
                //                 },
                //                 child: Container(
                //                   width: 70,
                //                   height: 35,
                //                   margin: const EdgeInsets.only(
                //                       top: 0, bottom: 10, right: 8),
                //                   decoration: BoxDecoration(
                //                     border: Border.all(
                //                         width: 1.0,
                //                         color: const Color(0xff1D1D1F)), //边框
                //                     color: Colors.white,
                //                     borderRadius: const BorderRadius.all(
                //                         Radius.circular(10.0)), //圆角
                //                   ),
                //                   alignment: Alignment.center,
                //                   child: const Text(
                //                     "查看物流",
                //                     style: TextStyle(
                //                         color: Color(0xff1D1D1F), fontSize: 13),
                //                   ),
                //                 ))),

                //         //待完成
                //         Visibility(
                //             visible: item["state"] == "DSH",
                //             child: GestureDetector(
                //                 onTap: () async {
                //                   // await showDialog(
                //                   //     context: context,
                //                   //     builder: (context) {
                //                   //       return buildDialog(
                //                   //           msg: "确认收货？",
                //                   //           okFn: () async {
                //                   //             await controller.doShopFinish(
                //                   //                 item['orderSn'].toString());
                //                   //             controller.onRefresh();
                //                   //           });
                //                   //     });
                //                 },
                //                 child: Container(
                //                   width: 70,
                //                   height: 35,
                //                   margin:
                //                       const EdgeInsets.only(bottom: 10, right: 8),
                //                   decoration: const BoxDecoration(
                //                     //边框
                //                     color: Color(0xffdf8e46),
                //                     borderRadius: BorderRadius.all(
                //                         Radius.circular(10.0)), //圆角
                //                   ),
                //                   alignment: Alignment.center,
                //                   child: const Text(
                //                     "确认收货",
                //                     style: TextStyle(
                //                         color: Colors.white, fontSize: 13),
                //                   ),
                //                 )))
                //       ],
                //     )
                //   ],
                // ),
                ),
            Container(
              width: double.infinity,
              padding: EdgeInsets.only(left: 10, right: 10),
              child: Row(
                children: [
                  Text(
                    "合计",
                    style: const TextStyle(
                        fontWeight: FontWeight.w500,
                        fontSize: 14,
                        color: Color(0xff666666)),
                  ),
                  SizedBox(
                    width: 5,
                  ),
                  Text(
                    "¥${showText(item["actualPrice"])}",
                    style: const TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 16,
                        color: Color(0xffFF3333)),
                  ),
                ],
              ),
            )
          ],
        ),
      ));
}
